<template>
  <div>
    <el-form
      :inline="true"
      :model="tag"
      class="demo-form-inline"
      style="margin-top: 25px; margin-left: 30%"
    >
      <el-form-item label="所属栏目">
        <el-select v-model="tag" placeholder="请选择栏目">
          <el-option label="通知" value="通知"></el-option>
          <el-option label="入学指南" value="入学指南"></el-option>
          <el-option label="日常生活" value="日常生活"></el-option>
          <el-option label="学业疑难" value="学业疑难"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selectBoardEssayByTag"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="articleList" border style="width: 100%">
      <el-table-column
        prop="nickName"
        label="发帖人昵称"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column label="标题" width="300" align="center">
        <template slot-scope="scope">
          <P>
            {{ scope.row.title }}
          </P>
        </template>
      </el-table-column>
      <el-table-column prop="tag" label="所属栏目" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="postTime"
        label="发布时间"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="readCount"
        label="阅读量"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="goodCount"
        label="点赞量"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="commentCount"
        label="评论量"
        width="80"
        align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="470" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.star === 0"
            icon="el-icon-circle-plus-outline"
            @click="addCream(scope.row.articleId)"
            >加精</el-button
          >
          <el-button
            type="success"
            size="mini"
            v-if="scope.row.star === 1"
            icon="el-icon-remove-outline"
            @click="cancleaddCream(scope.row.articleId)"
            >取消加精</el-button
          >
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.topType === 0"
            icon="el-icon-top"
            @click="addUpPost(scope.row.articleId)"
            >置顶</el-button
          >
          <el-button
            type="success"
            size="mini"
            v-if="scope.row.topType === 1"
            icon="el-icon-bottom"
            @click="cancelUpPost(scope.row.articleId)"
            >取消置顶</el-button
          >
          <router-link
            :to="'/lookpost/' + scope.row.articleId"
            style="margin: 0 10px"
          >
            <el-button type="primary" size="mini" icon="el-icon-more"
              >详情
            </el-button>
          </router-link>

          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="withdrawArticle(scope.row.articleId)"
            >撤回</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :total="totalRecords"
      :page-size="limit"
      :page-sizes="[9, 18, 27, 36, 45, 54, 63]"
      style="margin: 30px 0; text-align: center"
      layout="sizes,prev,pager,next,jumper,->,total,slot"
      @current-change="getStudentList"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import adminControll from "@/api/adminControll";
import forumApi from "@/api/forumApi";
import searchApi from "@/api/searchApi";
export default {
  data() {
    return {
      title: "",
      tag: "",
      currentPage: 1,
      totalRecords: 100,
      limit: 9,
      articleList: [
        {
          articleId: "1",
          nickName: "Xzh",
          title: "新版的射雕英雄传《金庸武侠世界》",
          tag: "通知",
          postTime: "2023-01-29 17:27:57",
          readCount: "5",
          goodCount: "10",
          commentCount: "15",
        },
        {
          articleId: "2",
          nickName: "Ssf",
          title: "ChatGPT国内镜像站初体验：聊天、Python代码生成等",
          tag: "入学指南",
          postTime: "2023-01-29 17:27:57",
          readCount: "6",
          goodCount: "12",
          commentCount: "18",
        },
        {
          articleId: "3",
          nickName: "Mike",
          title: "码农饭碗不保——ChatGPT正在取代Coder",
          tag: "日常生活",
          postTime: "2023-01-29 17:27:57",
          readCount: "7",
          goodCount: "14",
          commentCount: "21",
        },
        {
          articleId: "4",
          nickName: "Gy",
          title: "ChatGPT到底是个啥 - 它甚至会和狗说话",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
        {
          articleId: "5",
          nickName: "hsy",
          title: "他是00年的，我们卷不过他...",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
        {
          articleId: "6",
          nickName: "Gy",
          title: "2023美国大学生数学建模竞赛（美赛）思路&代码",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
        {
          articleId: "7",
          nickName: "Gy",
          title: "两年外包生涯做完，感觉自己废了一半....",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
        {
          articleId: "8",
          nickName: "Gy",
          title: "两年外包生涯做完，感觉自己废了一半....",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
        {
          articleId: "9",
          nickName: "Gy",
          title: "两年外包生涯做完，感觉自己废了一半....",
          tag: "学业疑难",
          postTime: "2023-01-29 17:27:57",
          readCount: "8",
          goodCount: "16",
          commentCount: "24",
        },
      ],
    };
  },

  components: {},

  computed: {},
  created() {
    this.selectBoardEssayByTag();
  },

  methods: {
    //搜索心声可了解帖子
    selectBoardEssayByTag() {
      searchApi
        .selectBoardEssayByTag("", this.tag, 0, this.currentPage, this.limit, 2)
        .then((response) => {
          this.articleList = response.data.records;
          this.totalRecords = response.data.total;
        });
    },
    //加精
    addCream(articleId) {
      this.$alert("是否加精该文章", "加精详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.starArticle(articleId, 1);
          }
        },
      });
    },
    //取消加精
    cancleaddCream(articleId) {
      this.$alert("是否取消加精该文章", "加精详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.starArticle(articleId, 0);
          }
        },
      });
    },
    //置顶文章
    addUpPost(articleId) {
      this.$alert("是否置顶该文章", "置顶详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.topArticle(articleId, 1);
          }
        },
      });
    },
    //取消置顶
    cancelUpPost(articleId) {
      this.$alert("是否取消置顶该文章", "置顶详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.topArticle(articleId, 0);
          }
        },
      });
    },
    changeSize(size) {
      this.limit = size;
      this.getStudentList(1);
    },
    //撤回文章
    withdrawArticle(articleId) {
      this.$alert("您确定撤回该文章吗", "撤回详情", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") this.deleteByArticleId(articleId);
        },
      });
    },

    starArticle(articleId, type) {
      adminControll.starArticle(articleId, type).then((response) => {
        this.selectBoardEssayByTag();
      });
    },
    topArticle(articleId, type) {
      adminControll.topArticle(articleId, type).then((response) => {
        this.selectBoardEssayByTag();
      });
    },
    deleteByArticleId(articleId) {
      adminControll.deleteByArticleId(articleId).then((response) => {
        this.selectBoardEssayByTag();
      });
    },
  },
};
</script>
<style scoped>
img {
  display: block;
  width: 75px;
  height: 75px;
}
.exceed {
  color: red;
}
p {
  width: 250px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
